<template>
  <router-view></router-view>
</template>
<script lang="ts">
import Vue from 'vue';
import { Component, Watch } from 'vue-property-decorator';
import { sessionStorageSet, sessionStorageGet } from '@/utils/tools';
import * as Cookies from 'js-cookie';

@Component({
  components: {},
})
export default class Home extends Vue {
  created() {
    Cookies.set('sid', '68435980c4460350623192b7f9e36b4f852b3');
    if (sessionStorageGet('store')) {
      this.$store.replaceState(Object.assign({}, this.$store.state, JSON.parse(sessionStorageGet('store') as any)));
    }

    //在页面刷新时将vuex里的信息保存到sessionStorage里
    window.addEventListener('beforeunload', () => {
      sessionStorageSet('store', JSON.stringify(this.$store.state));
    });
  }
  mounted() {
    this.initPage();
  }

  @Watch('$route', { deep: true })
  async onChangeRoute(to: any, from: any) {}

  async initPage() {
    // 国际化: zh_CN中文 en_US 英文
    if (!Cookies.get('_JARVIS.LOCALE_')) {
      const hosts = location.hostname.split('.');
      const domain = `.${hosts.slice(hosts.length - 2).join('.')}`;
      Cookies.set('_JARVIS.LOCALE_', 'zh_CN', { domain, path: '/' });
    }
    const nowLg: string = Cookies.get('_JARVIS.LOCALE_') as string;
    const lang = nowLg === 'en_US' ? 'en' : 'zh';
    // const lang = 'zh'; // 英文化暂未处理，默认中文
    this.$i18n.locale = lang;
    this.$store.commit('setLang', lang);
  }
}
</script>
